package z.tool.service.wrappedcall;

import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import z.tool.service.ServiceResult;

/**
 * 
 * @author auzll@163.com
 */
final class CallRunner implements Runnable  {
    private static final Log LOG = LogFactory.getLog(CallRunner.class);
    
    private CountDownLatch latch;
    private Object targetObj;
    private Method targetMethod;
    private Object[] args;
    private ServiceResult result;
    
    public CallRunner(ServiceResult result, CountDownLatch latch, Object targetObj, Method targetMethod, Object... args) {
        this.result = result;
        this.latch = latch;
        this.targetObj = targetObj;
        this.targetMethod = targetMethod;
        this.args = args;
    }
    
    public void run() {
        try {
            result.success(true).value(targetMethod.invoke(targetObj, args));
        } catch (Exception e) {
            LOG.error("method:run", e);
            result.success(false).exception(e);
        } finally {
            latch.countDown();
        }
    }
}
